package com.yifeng.repo.controller.fadada.web;

import com.gomcarter.frameworks.base.json.ErrorCode;
import com.google.common.base.Strings;
import com.yifeng.repo.base.utils.servlet.RequestHelper;
import com.yifeng.repo.base.utils.servlet.ResponseHelper;
import com.yifeng.repo.controller.fadada.constant.AccountTypeEnum;
import com.yifeng.repo.controller.fadada.signature.FadadaSignatureManager;
import com.yifeng.repo.controller.fadada.signature.dto.QuickSignData;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by daibing on 2024/12/04.
 */
@Slf4j
public class FadadaServlet extends HttpServlet {
    public static final String REGISTER_ACCOUNT = "/registerAccount";
    public static final String ADD_SIGNATURE = "/addSignature";
    public static final String PERSON_QUICKSIGN = "/personQuickSign";
    public static final String COMPANY_QUICKSIGN = "/companyQuickSign";

    private final FadadaSignatureManager fadadaSignatureManager;

    public FadadaServlet(FadadaSignatureManager fadadaSignatureManager) {
        this.fadadaSignatureManager = fadadaSignatureManager;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        try {
            if (uri.endsWith(REGISTER_ACCOUNT)) {
                AccountTypeEnum accountType = AccountTypeEnum.resolve(req.getParameter("accountType"));
                String openId = req.getParameter("openId");
                String customerId = fadadaSignatureManager.registerAccount(accountType, openId);
                ResponseHelper.doWrite(resp, customerId);
                return;
            }
            if (uri.endsWith(ADD_SIGNATURE)) {
                String customerId = req.getParameter("customerId");
                String imgUrl = req.getParameter("imgUrl");
                String signatureId = fadadaSignatureManager.addSignature(customerId, imgUrl);
                ResponseHelper.doWrite(resp, signatureId);
                return;
            }
            if (uri.endsWith(PERSON_QUICKSIGN)) {
                String transactionId = req.getParameter("transactionId");
                String customerId = req.getParameter("customerId");
                String customerName = req.getParameter("customerName");
                String customerIdentNo = req.getParameter("customerIdentNo");
                String contractId = req.getParameter("contractId");
                String mobile = req.getParameter("mobile");
                QuickSignData signData = fadadaSignatureManager.personQuickSign(transactionId, customerId, customerName, customerIdentNo, contractId);
                if (!Strings.isNullOrEmpty(mobile)) {
                    fadadaSignatureManager.pushShortUrlSms(signData.getUrl(), mobile);
                }
                ResponseHelper.doWrite(resp, signData);
                return;
            }
            if (uri.endsWith(COMPANY_QUICKSIGN)) {
                String transactionId = req.getParameter("transactionId");
                String customerId = req.getParameter("customerId");
                String companyName = req.getParameter("companyName");
                String companyCreditNo = req.getParameter("companyCreditNo");
                String contractId = req.getParameter("contractId");
                String mobile = req.getParameter("mobile");
                QuickSignData signData = fadadaSignatureManager.companyQuickSign(transactionId, customerId, companyName, companyCreditNo, contractId);
                if (!Strings.isNullOrEmpty(mobile)) {
                    fadadaSignatureManager.pushShortUrlSms(signData.getUrl(), mobile);
                }
                ResponseHelper.doWrite(resp, signData);
                return;
            }
            ResponseHelper.doError(resp, ErrorCode.noPermission.getCode(),
                    "http.method_post_not_supported from " + RequestHelper.getRemoteAddress(req));
        } catch (Throwable t) {
            log.error("fadada doPost error: ", t);
            ResponseHelper.doError(resp, ErrorCode.systemInternalError.getCode(), t.getMessage());
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

}
